<template>
    <div class="page">
        <div class="weui-header bg-green" style="position: fixed;top: 0; width: 100%;z-index: 500;"> 
            <div class="weui-header-left" @click="goBack">
                <svg class="icon white" aria-hidden="true">
                    <use xlink:href="#icon-fanhui"></use>
                </svg>
            </div>
            <h1 class="weui-header-title">选择测试范围</h1>
        </div>
        <div style="height:50px"></div>
        <TreeList
            :dataList='lists'
            value-key='CLASS_CODE'
            label-key='CLASS_INFO'
            :hasCheckBox='true'
            :hasArrow="false"
        />   
        <div style="height:80px"></div>

        <div class="page__ft">
            <div class="weui-footer weui-footer_fixed-bottom">
                <a href="javascript:" role="button" class="weui-btn weui-btn_primary" style="color:#fff" @click="toNextPage">下一步</a>
            </div>
        </div>
    </div>
</template>

<script>
import TreeList from '../../components/TreeList.vue';
export default {
    components: {
      TreeList
    },
    data () {
        return {
            selectVal: {},
            lists: [],
            checkedList:[]
        }
    },
    methods: {
        goBack(){
            this.$router.go(-1);
        },
         toNextPage(){

            this.getChecked();
            if(this.checkedList.length>0){
                this.$store.commit('setcreateTestRequest',{
                    TEST_TIME:60        ,
                    QUESTION_NUMBER:100 ,
                    CLASS_LIST:this.checkedList
                });

                this.$router.push({name:'SetNumber'});
            }
            else{
                this.$weui.alert('至少勾选一个题库');
            }
        },
        //获取选中
        getChecked(){
            this.checkedList=[];
            this.lists.forEach((item)=>{
                this.getTreeNodeChecked(item);
            })
        },
        getTreeNodeChecked(treeNode){
            if(treeNode.checked==="checked"){
                this.checkedList.push(treeNode.CLASS_CODE);                
            }
            if(treeNode.children && treeNode.children.length>0){
                treeNode.children.forEach((item)=>{
                    this.getTreeNodeChecked(item);
                })
            }
        },
        //获取数据
        getTreeList () {
            var loading           = this.$weui.loading('数据处理中…');
            this.$api.Training.GetClassTree().then((msg)=>{
                loading.hide();
                if(msg.data.R){
                    this.lists = msg.data.D;
                }
                else{
                    this.$weui.alert(msg.data.I);
                }
            }).catch((response)=> {
               loading.hide();
                // 响应错误回调
                this.$weui.alert("调用http错误，代码:" + response.message);
            });  
        }
    },
    created  () {        
        document.title="题库分组"  
        this.getTreeList();
    }
}
</script>

<style>

</style>